package phapp.service.common;

import java.io.*;
import java.util.*;
import java.io.File;

public class CommonUtils
{
   private static final int SEQ_MAX_VALUE   = 999999;
   private static final int SEQ_MIN_VALUE   = 100000;

   private static Integer SynSeqLock = new Integer(0);
   private static int msgSEQ = 0;

   public CommonUtils()
   {
   }

//   public static String getSystemParam(String strParamName)
//   {
//      Map mapResult = null;
//      List listResult = null;
//      String strValue = "", strSQL = "";
//
//      strSQL = "select param_value from t_static_sysparam where param_name='" + strParamName + "'";
//      listResult = ApacheDBUtils.execDBQuery(strSQL);
//      if(listResult.size() > 0)
//      {
//         mapResult = (Map) listResult.get(0);
//         strValue = (String) mapResult.get("param_value");
//      }
//      else
//      {
//         strValue = null;
//      }
//      listResult.clear();
//      listResult = null;
//
//      return strValue;
//   }

   public static String generateSeq()
   {
      String strSeq = "";

      synchronized (SynSeqLock)
      {
         msgSEQ++;
         if(msgSEQ > SEQ_MAX_VALUE || msgSEQ < SEQ_MIN_VALUE)
            msgSEQ = SEQ_MIN_VALUE;

         strSeq = formatDateTime() + Integer.toString(msgSEQ);
      }

      return strSeq;
   }

   public static String formatDateTime()
   {
      Calendar now = Calendar.getInstance();
      String year = Integer.toString(now.get(Calendar.YEAR));
      String mon = Integer.toString(now.get(Calendar.MONTH) + 1);
      String day = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
      String hour = Integer.toString(now.get(Calendar.HOUR_OF_DAY));
      String min = Integer.toString(now.get(Calendar.MINUTE));
      String sec = Integer.toString(now.get(Calendar.SECOND));

      mon = (mon.length() == 1) ? "0" + mon : mon;
      day = (day.length() == 1) ? "0" + day : day;
      hour = (hour.length() == 1) ? "0" + hour : hour;
      min = (min.length() == 1) ? "0" + min : min;
      sec = (sec.length() == 1) ? "0" + sec : sec;

      return (year + mon + day + hour + min + sec);
   }

   public static int saveFileData(String strFile, byte[] btFile)   
   {
      int nResult = 0;
      String strDestFile = "";
      OutputStream osFile = null;

      try
      {
         strDestFile = strFile;
         osFile = new FileOutputStream(strDestFile);

         osFile.write(btFile);
      }
      catch (Exception eLoad)
      {
         nResult = -1;
         eLoad.printStackTrace();
      }
      finally
      {
         try
         {
            if(osFile != null) osFile.close();
         }
         catch (Exception eOS) { }

         osFile = null;
      }

      return nResult;
   }
   
   public static int saveUploadFile(File fileSrc, String strSaveFile)
   {
      int nResult = 0;
      String strDestFile = "";
      InputStream isFile = null;
      OutputStream osFile = null;

      try
      {
         strDestFile = strSaveFile;
         isFile = new FileInputStream(fileSrc);
         osFile = new FileOutputStream(strDestFile);

         int nCount = 0;
         byte[] btData = null;
         btData = new byte[16 * 1024];
         do
         {
            nCount = isFile.read(btData, 0, 8192);
            if(nCount > 0) osFile.write(btData, 0, nCount);
         }
         while (nCount != -1);

      }
      catch (Exception eLoad)
      {
         nResult = -1;
         eLoad.printStackTrace();
      }
      finally
      {
         try
         {
            if(isFile != null) isFile.close();
         }
         catch (Exception eIS) { }
         try
         {
            if(osFile != null) osFile.close();
         }
         catch (Exception eOS) { }

         isFile = null;
         osFile = null;
      }

      return nResult;
   }
   
}
